/*
*	(*^。^*) TFrameWork License (*^。^*)
*/

/*
 *  Title :  
 *          
 *  Description :  
 *          
 *          
 *  Date : 
 *
 *  Author : DengPing
 *
 *  Modify : 
 */

using UnityEngine;
using UnityEngine.Serialization;

namespace TFrameWork.Module.ActionEditor
{
    public class PlayerController : MonoBehaviour
    {
        [FormerlySerializedAs("_skillConfig")] public ActionData actionData;
        private TimeLineAction _timeLineAction;

        private CharacterController characterController;
        public float speed = 5.0f;
        public float gravity = 20.0f;

        private Vector3 moveDirection = Vector3.zero;

        private void Start()
        {
            characterController = GetComponent<CharacterController>();
            _timeLineAction = GetComponent<TimeLineAction>();
        }

        private void Update()
        {
            // 获取输入
            float horizontal = Input.GetAxis("Horizontal");
            float vertical = Input.GetAxis("Vertical");

            // 将输入转换为世界空间下的移动方向
            Vector3 forward = transform.forward * vertical;
            Vector3 right = transform.right * horizontal;
            Vector3 moveDirection = (forward + right).normalized;

            // 设置角色移动方向和速度
            moveDirection *= speed;

            // 处理角色的跳跃
            if (characterController.isGrounded && Input.GetKeyDown(KeyCode.Space))
            {
                moveDirection.y = Mathf.Sqrt(20 * gravity);
            }

            // 应用重力
            moveDirection.y -= gravity * Time.deltaTime;

            // 应用移动
            characterController.Move(moveDirection * Time.deltaTime);


            if (Input.GetKeyDown(KeyCode.K))
            {
                _timeLineAction.PlaySkill(actionData, SkillEndCallBack);
            }
        }

        private void SkillEndCallBack()
        {
            Debug.Log("技能完成回调");

        }
    }
}
